//
//  THTaskHallVC.m
//  YJB
//
//  Created by YJB on 2016/12/16.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THTaskHallVC.h"
#import "THTaskCell.h"
#import "masonry.h"
#import "YJUserDefaultTool.h"
#import "YJNetworkTool.h"
#import "THTaskModel.h"
#import "THTaskReportVC.h"
#import "THTaskPublishVC.h"
#import "THSubjectTaskVC.h"

@interface THTaskHallVC ()
@property(nonatomic,strong)UIView *headView;
@property(nonatomic,strong)NSMutableArray *tasks;
@end

@implementation THTaskHallVC
- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.title = @"任务列表";
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = kUIColorFromRGB(0xfff5f0);
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[THTaskCell class] forCellReuseIdentifier:taskCellID];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.tableHeaderView = self.headView;
    
}


- (UIView *)headView
{
    if (_headView == nil)
    {
        _headView = [[UIView alloc] init];
        _headView.backgroundColor = WHITECOLOR;
        _headView.height = 85;
        UILabel *label = [[UILabel alloc] init];
        [_headView addSubview:label];
        label.textColor = kUIColorFromRGB(0x999999);
        label.font = FONT(14);
        label.text = @"点击按钮发布新任务";
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(200);
            make.top.mas_equalTo(15);
        }];
        
        UILabel *tip = [[UILabel alloc]init];
        tip.text = @"官方提示:";
        tip.textAlignment = NSTextAlignmentCenter;
        tip.backgroundColor = kUIColorFromRGB(0xf4f3f3);
        tip.font = FONT(14);
        tip.textColor = kUIColorFromRGB(0xd01e06);
        [_headView addSubview:tip];
        [tip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(label.mas_bottom).offset(10);
            make.height.mas_equalTo(18);
            make.width.mas_equalTo(86);
        }];
        
        UILabel *tipTwo = [[UILabel alloc]init];
        tipTwo.text = @"        如要求出钱或发短信才能完成的任务,请不要做,谢谢!";
        tipTwo.font = FONT(12);
        tipTwo.backgroundColor = kUIColorFromRGB(0xf4f3f3);
        tipTwo.textColor = kUIColorFromRGB(0x222222);
        [_headView addSubview:tipTwo];
        [tipTwo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(0);
            make.top.mas_equalTo(tip.mas_bottom);
        }];
        
        UIButton *btn = [[UIButton alloc] init];
        [btn setImage:Image(@"添加按钮") forState:UIControlStateNormal];
        btn.titleLabel.font = FONT(16);
        [_headView addSubview:btn];
        [btn addTarget:self action:@selector(addNewTask) forControlEvents:UIControlEventTouchUpInside];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-20);
            make.height.height.mas_equalTo(50);
            make.centerY.mas_equalTo(_headView.mas_centerY);
        }];
    }
    return _headView;
}

- (void)loadData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *userName = [YJUserDefaultTool readUserName];
    NSString *md5Key = [YJUserDefaultTool readKeyID];
    params[@"action"] = @"get_all_list";
    params[@"makegold"] = @{@"username":userName,
                            @"firstrow":@"-1",
                            @"listrows":@"",
                            @"user_info":userName,
                            @"phone_info":@"iOS",
                            @"md5_key":md5Key};
    [YJNetworkTool postWithUrl:THURL(@"cpcclick.php") parameters:params success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        NSString *code = responseObject[@"return"][@"code"];
        if (code.integerValue == 200)
        {
            NSArray *array = responseObject[@"return"][@"data"][@"list"];
            _tasks = [THTaskModel mj_objectArrayWithKeyValuesArray:array];
        }
        else if(code.integerValue == 500)
            [_tasks removeAllObjects];
        [self.tableView reloadData];
        
        NSLog(@"loadData:%@",responseObject);
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
    }];
}

#pragma mark ----------------------- 表格代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tasks.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    THTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:taskCellID forIndexPath:indexPath];
    cell.task = _tasks[indexPath.row];
    return cell;
}

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    THTaskModel *model = _tasks[indexPath.row];
    YJWeakSelf
    UITableViewRowAction *taskAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"做任务" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        THSubjectTaskVC *taskVC = [[THSubjectTaskVC alloc] init];
        taskVC.pubid = model.ID;
        [weakSelf.navigationController pushViewController:taskVC animated:YES];
        [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
//        THDoTaskVC *taskVC = [[THDoTaskVC alloc]init];
//        taskVC.model = model;
//        [weakSelf.navigationController pushViewController:taskVC animated:YES];
//        [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
    taskAction.backgroundColor = kUIColorFromRGB(0xfa5f3c);
    UITableViewRowAction *reportAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"举报" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        THTaskReportVC *reportVC = [[THTaskReportVC alloc]init];
        reportVC.taskTitle = model.title;
        reportVC.ID = model.ID;
        reportVC.taskType = @"2";
        [weakSelf.navigationController pushViewController:reportVC animated:YES];
        [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
    reportAction.backgroundColor = kUIColorFromRGB(0xf5a741);
    return @[taskAction,reportAction];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    THCpcHallModel *model = _tasks[indexPath.row];
//    THDoTaskVC *taskVC = [[THDoTaskVC alloc]init];
//    taskVC.model = model;
//    [self.navigationController pushViewController:taskVC animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = kUIColorFromRGB(0xfff5f0);
    
    UIView *lineOne = [[UIView alloc]init];
    lineOne.backgroundColor = kUIColorFromRGB(0xcccccc);
    [headerView addSubview:lineOne];
    [lineOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    UIView *lineTwo = [[UIView alloc]init];
    lineTwo.backgroundColor = kUIColorFromRGB(0xcccccc);
    [headerView addSubview:lineTwo];
    [lineTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    NSArray *titles = @[@"任务标题",@"普通奖励",@"VIP奖励"];
    NSArray *widths = @[@(IPHONE_W - 18 * GRIDW),@(9 * GRIDW),@( 9 * GRIDW)];
    UILabel *lastLab = nil;
    for (int i = 0; i < titles.count; i ++)
    {
        UILabel *lab = [[UILabel alloc]init];
        lab.textColor = kUIColorFromRGB(0x333333);
        lab.font = FONT(15);
        lab.text = titles[i];
        lab.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0.5);
            make.bottom.mas_equalTo(-0.5);
            make.width.equalTo(widths[i]);
            if (lastLab)
                make.left.mas_equalTo(lastLab.mas_right);
            else
                make.left.mas_equalTo(0);
        }];
        lastLab = lab;
        //vip图标
        if (i == 2)
        {
            UIImageView *vip = [[UIImageView alloc]initWithImage:Image(@"vip_icon")];
            [lab addSubview:vip];
            [vip mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(8);
                make.centerX.mas_equalTo(lab.mas_centerX).offset(33);
            }];
        }
    }
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (void)addNewTask
{
    THTaskPublishVC *taskVC = [[THTaskPublishVC alloc]init];
    [self.navigationController pushViewController:taskVC animated:YES];
}

@end
